
local ccxl = 52500
AddComponentPostInit("combat", function(Combat)
 
    local oldGetAttacked = Combat.GetAttacked
    function Combat:GetAttacked(attacker, damage, weapon, stimuli, spdamage, ...)
        if not attacker or not attacker:IsValid() or not attacker:HasTag("i26") or self.inst.components.health == nil then
            return oldGetAttacked(self, attacker, damage, weapon, stimuli, spdamage, ...)
        end
        if self.inst.components.health:IsDead() then return true end 

        if attacker.i26onbattle_task ~= nil then
            attacker.i26onbattle_task:Cancel()
            attacker.i26onbattle_task = nil
        end
        attacker.i26onbattle_task = attacker:DoTaskInTime(5, function()
            attacker.i26onbattle_task = nil
        end)

        local spds = nil
        local attacker_combat = attacker.components.combat
        if spdamage ~= nil and next(spdamage) then
            spds = {}
            local damagemult = 1      
                * math.max(1, self.externaldamagetakenmultipliers:Get()) 
                * math.max(1, attacker_combat.damagemultiplier)
                * math.max(1, attacker_combat.externaldamagemultipliers:Get())

            for k,v in pairs(spdamage) do
                spds[k] = v * damagemult
            end
        end
        
        if attacker.YunLiFM then
            local hps = self.inst.components.health.maxhealth 
            hps = hps / math.clamp((1-self.inst.components.health.absorb), .01, 1)
            hps = hps / math.clamp((1-self.inst.components.health.externalabsorbmodifiers:Get()), .01, 1)
            hps = hps / self.externaldamagetakenmultipliers:Get()

            --hps
            if self.inst.components.health.penalty <= 0 then
                local apmult
                if hps > ccxl then
                    local mustclear = hps - ccxl
                    apmult = math.max((math.min(mustclear / ccxl, 2)), .2) / 10
                else
                    apmult = .02
                end
                self.inst.components.health.penalty = math.min(self.inst.components.health.penalty + apmult, .999999)
            end

            --real
            if not self.inst.components.health:IsInvincible() then
                local frdmg = self.inst.components.health.maxhealth / 1000
                -- frdmg = frdmg 
                --     * math.max(1, self.externaldamagetakenmultipliers:Get()) 
                --     * math.max(1, attacker_combat.damagemultiplier)
                --     * math.max(1, attacker_combat.externaldamagemultipliers:Get())
                -- TheNet:Say(frdmg)
                -- self.inst.components.health:SetVal(self.inst.components.health.currenthealth - frdmg, "realsp")
                self.inst.components.health:DoDelta(-frdmg, nil, nil, false, nil, true)
            end
            
            --dead
            if attacker.components.health:IsDead() then
                attacker:PushEvent("killed", { victim = self.inst, attacker = attacker })
                if self.onkilledbyother ~= nil then
                    self.onkilledbyother(self.inst, attacker)
                end
                return true
            end
            
            --yunli hp damagemult
            local losehp = attacker.components.health.maxhealth - attacker.components.health.currenthealth
            if losehp > 0 then
                local damagemult = 1 + (losehp * .02)

                damage =  damage * damagemult
                if spds ~= nil then
                    for k,v in pairs(spds) do
                        v = v * damagemult
                        --TheNet:Say(v)
                    end
                end

            end
            
        end



        -- spdamage

        -- local oridmg = damage or 0
        -- local orihp = self.inst.components.health.currenthealth
        -- if spdamage ~= nil and next(spdamage) then
        --     for k, v in pairs(spdamage) do
        --         oridmg = oridmg + v
        --     end
        -- end

        --local oreturn, xxx = oldGetAttacked(self, attacker, damage, weapon, stimuli, spdamage, ...)

        -- if self.inst.components.health then
        --     local realdmg = math.max(orihp - self.inst.components.health.currenthealth, .1)
        --     --TheNet:Say(oridmg.."_"..realdmg)
        --     if oridmg / realdmg > 1.4 and attacker and attacker:IsValid() then
        --         attacker:PushEvent("i26xushi")
        --     end
        -- end

        --return oreturn, xxx

        return oldGetAttacked(self, attacker, damage, weapon, stimuli, spds, ...)
    end


end)